//
//  InfoViewController.m
//  Yogofly
//
//  Created by Meracom on 6/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "InfoViewController.h"
#import "PaymentViewController.h"
#import "UINavigationBar+CustomImage.h"
#import "Utility.h"
#import "AppDelegate.h"

@implementation InfoViewController

@synthesize tbarKeyboard;

#define kFieldTag_Phone 1
#define kFieldTag_Name  2
#define kFieldTag_Email  3

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Step 3";
    
    [[Utility Shared] loadNavigationIOS5];
    
    for (UITextField *txt in self.view.subviews) {
        if ([txt isKindOfClass:[UITextField class]]) {
            txt.inputAccessoryView = self.tbarKeyboard;
        }
    }
    UIImage *imgHome = [UIImage imageNamed:@"iconRight.png"];
    UIButton *btnHome = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnHome setBackgroundImage:imgHome forState:UIControlStateNormal];
    [btnHome addTarget:self action:@selector(onHome) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btnRight = [[UIBarButtonItem alloc] initWithCustomView:btnHome];
    self.navigationItem.rightBarButtonItem= btnRight;
    btnRight.customView.frame = CGRectMake(0.0f, 0.0f, imgHome.size.width-24, imgHome.size.height-15);
    [btnRight release];
    
    UIImage *imgBack = [UIImage imageNamed:@"iconLeft.png"];
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setBackgroundImage:imgBack forState:UIControlStateNormal];
    [btnBack addTarget:self action:@selector(onBack) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btnLeft = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
    self.navigationItem.leftBarButtonItem= btnLeft;
    btnLeft.customView.frame = CGRectMake(0.0f, 0.0f, imgBack.size.width-24, imgBack.size.height-15);
    [btnLeft release];
    // Do any additional setup after loading the view from its nib.
}

- (void)onBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)onHome{
    [[AppDelegate navigationControllerInstance] popToRootViewControllerAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)showPaymentView:(id)sender{
    PaymentViewController *vcPayment = [[PaymentViewController alloc] initWithNibName:@"PaymentViewController" bundle:nil];
    [self.navigationController pushViewController:vcPayment animated:YES];
    [vcPayment release];
}

- (IBAction)onCloseKeyboard:(id)sender{
    for (UITextField *txt in self.view.subviews) {
        if ([txt isKindOfClass:[UITextField class]]) {
            [txt resignFirstResponder];
        }
    }
    if (self.view.frame.origin.y != 0) {
        [self moveViewAnimation:0];
    }    
}

- (void)moveViewAnimation : (NSInteger)pam {
    [UIView beginAnimations: @"moveField_Date" context: nil];
    [UIView setAnimationDelegate: self];
    [UIView setAnimationDuration: 0.25f];
    [UIView setAnimationCurve: UIViewAnimationCurveEaseInOut];
    
    self.view.frame = CGRectMake(0, pam, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag == kFieldTag_Name) {
        [self moveViewAnimation:-80];
    }
    if (textField.tag == kFieldTag_Email) {
        [self moveViewAnimation:-180];
    }
}

@end
